import 'package:flutter/cupertino.dart';
import 'package:flutter_learn_vi/src/bean/bean_problem.dart';
import 'package:get/get.dart';

import '../../../net/dio_utils.dart';

class AuestionErrorController extends GetxController {
  List<ProblemModel> requestList = [];
  int _currentIndex = 0;

  //错题加载完成后回调
  void onTopicList(List<ProblemModel> list) {
    requestList = list;
    update();
  }

  double get currentIndex => _pageController.page ?? 0;
  PageController _pageController = new PageController();

  PageController get pageController => _pageController;

  void previousPage() {
    _pageController.previousPage(
      duration: Duration(milliseconds: 200),
      curve: Curves.easeInSine,
    );
    update(["collectFunction"]);
  }

  void nextPage() {
    _pageController.nextPage(
      duration: Duration(milliseconds: 200),
      curve: Curves.easeInSine,
    );
    update(["collectFunction"]);
  }

  void selectPage(int index) {
    _pageController.jumpToPage(index);
    update();
  }

  List<int> collection = [];

  bool getCurrentIsCollection() {
    double current = _pageController.page ?? 0;
    if (requestList.length == 0) {
      return false;
    }
    ProblemModel model = requestList[current.toInt()];
    return collection.contains(model.id);
  }

  Future<bool> collectFunction() async {
    double current = _pageController.page ?? 0;

    ProblemModel model = requestList[current.toInt()];
    collection.add(model.id);
    Map<String, dynamic> map = {
      "newsId": model.id,
      "title": model.title.replaceAll("<p>", "").replaceAll("<\/p>", ""),
      "flag": 0,
      "collectType": "question"
    };
    ResponseInfo partResponseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.addNewsCollect,
      queryParameters: map,
    );
    if (partResponseInfo.success) {
      update(["collectFunction"]);
    } else {}

    return Future.value(true);
  }
}
